using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

namespace Games.Frameworks.UIParticle
{
    [ExecuteAlways]
    public class UIParticle : MonoBehaviour
    {
        public RawImage rawImage;
        public ParticleSystem Particle =>GetComponentInChildren<ParticleSystem>(true);
        
        [SerializeField]private UIParticleHelper _helper;
        public bool OnPlay=>_helper!=null ?_helper.use: false;
        protected  void Awake()
        {
            rawImage = GetComponent<RawImage>();
        }

        protected  void OnEnable()
        {
            _helper = null;
            EditorAndPlay();
            Debug.Log(gameObject.name+"UIParticleOnEnable 准备完成"+_helper?.Id+OnPlay);
        }

        public void CheckInit()
        {
            rawImage =rawImage ?? GetComponent<RawImage>();
        }
        
        public void EditorAndPlay()
        {
            if (!OnPlay&&Particle!=null)
            {
                Debug.Log(gameObject.name+"可以播放 准备播放begin");
                CheckInit();
                _helper=UIParticleManager.instance.ParticlePlay(this);
            }
        }
        
        public void Save()
        {
            if (OnPlay)
            {
                UIParticleManager.instance.EditorComplete(_helper);
                _helper = null;
                //保存预制体
                if (PrefabUtility.IsPartOfPrefabInstance(gameObject))
                {
                    var prefabRoot=PrefabUtility.GetOutermostPrefabInstanceRoot(gameObject);
                    PrefabUtility.ApplyPrefabInstance(prefabRoot,InteractionMode.UserAction);
                }
            }
        }
        
        public void Stop()
        {
            if (OnPlay)
            {
                UIParticleManager.instance.ParticleStop(_helper);
                _helper = null;
            }
        }
        
        public void SaveAndEditor()
        {
            if (OnPlay)
            {
                Save();
            }
            EditorAndPlay();
        }
        
        public ParticleSystem FindRealParticle()
        {
            if ( _helper != null)
            {
                var particle = _helper.gameObject.GetComponentInChildren<ParticleSystem>();
                return particle;
            }

            return null;
        }


        protected  void OnDisable()
        {
            bool IsDestroy() => gameObject.activeSelf;
            if (OnPlay)
            {
                if(Application.isPlaying)
                    UIParticleManager.instance.ParticleStop(_helper,true);
                else
                {
                    UIParticleManager.instance.ParticleStop(_helper);
                }
                /*if(!IsDestroy())
                    UIParticleManager.instance.ParticleStop(_helper,true);
                else
                    UIParticleManager.instance.ParticleStop(_helper);*/
            }
        }
    }
}

